package appapi.service.impl;

import appapi.Global.BusinessException;
import appapi.Request.record.FaceRecordReq;
import appapi.Vo.brider.FaceRecordVo;
import appapi.common.ResponseBean;
import appapi.dto.bridge.FaceRecordDto;
import appapi.dto.bridge.FaceRecordExportDto;
import appapi.entity.Zhongjiao.FaceRecordPageReq;
import appapi.service.mango.FaceRecordService;
import appapi.utils.BeanCopyUtils;
import appapi.utils.ExcelUtils;
import appapi.utils.RedisUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import appapi.dao.FaceRecordMapper;
import appapi.entity.bridge.FaceRecordEntity;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author dhq
 * @date 2023/9/11  12:26
 * @version 1.0
 */
@Service
public class FaceRecordServiceImpl implements FaceRecordService {

    @Resource
   FaceRecordMapper faceRecordMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return faceRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(FaceRecordEntity record) {
        return faceRecordMapper.insert(record);
    }

    @Override
    public int insertSelective(FaceRecordEntity record) {
        return faceRecordMapper.insertSelective(record);
    }

    @Override
    public FaceRecordEntity selectByPrimaryKey(Long id) {
        return faceRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(FaceRecordEntity record) {
        return faceRecordMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(FaceRecordEntity record) {
        return faceRecordMapper.updateByPrimaryKey(record);
    }

    /**
     * 分页获取人脸识别记录
     * @param faceRecordPageReq
     * @return
     */
    @Override
    public List<FaceRecordVo> getFaceRecordListByPage(FaceRecordPageReq faceRecordPageReq) {
        PageHelper.startPage(faceRecordPageReq.getCurrPage(), faceRecordPageReq.getPageSize());
        List<FaceRecordVo> faceRecordVoList = faceRecordMapper.getFaceRecordListByPage(faceRecordPageReq);
        return faceRecordVoList;
    }

    @Override
    public FaceRecordVo getDetail(Long id) {
        return faceRecordMapper.getDetail(id);
    }


    @Autowired
    private RedisUtils redisUtils;

    @Override
    public void redis(MultipartFile file) throws IOException {
        List<FaceRecordExportDto> list = ExcelUtils.importExcel(file, 1, 1, true, FaceRecordExportDto.class);
        if (list == null) {
           throw new BusinessException(500,"数据为空");
        }
        if (list.size() == 0) {
            throw new BusinessException(500,"数据为空");
        }
        for (FaceRecordExportDto dto : list) {
//            FaceRecordEntity entity = new FaceRecordEntity();
//            BeanUtils.copyProperties(dto,entity);
//            System.out.println("++++++++++++++"+entity);
//            FaceRecordEntity entity = BeanCopyUtils.copyBean(dto, FaceRecordEntity.class);
            redisUtils.setHash("import1",dto.getOperId().toString(),dto);
        }
    }




    public List<FaceRecordVo> getFaceRecordList(FaceRecordReq req) {
        List<FaceRecordVo> faceRecordVoList = faceRecordMapper.selectFaceRecordList(req);
        return faceRecordVoList;
    }

}
